package net.tensor.tensorprovider.resolve;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import graphql.kickstart.tools.GraphQLQueryResolver;
import lombok.AllArgsConstructor;
import net.tensor.tensorprovider.database.entity.ActRuTask;
import net.tensor.tensorprovider.database.entity.TmpActUser;
import net.tensor.tensorprovider.database.service.impl.ActRuTaskServiceImpl;
import net.tensor.tensorprovider.database.service.impl.TmpActUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Q
 */
@Component
@AllArgsConstructor
public class Query implements GraphQLQueryResolver {
    @Autowired
    TmpActUserServiceImpl tmpActUserService;
    @Autowired
    ActRuTaskServiceImpl actRuTaskService;

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public List<TmpActUser> findUserList(int current,int size,TmpActUser tmpActUser) {
        return tmpActUserService.page(new Page<>(current,size), new QueryWrapper<>(tmpActUser)).getRecords();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public List<ActRuTask> actRuTaskList(int current,int size,ActRuTask actRuTask) {
        return actRuTaskService.page(new Page<>(current,size), new QueryWrapper<>(actRuTask)).getRecords();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public ActRuTask findRuTaskById(String id) {
        return actRuTaskService.getById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public Map<String, Object> getPayload(JSONObject jsonObject) {
        Map<String, Object> payload = new HashMap<>();
        for (String key :
                jsonObject.keySet()) {
            if (jsonObject.get(key) != null && !"".equals(jsonObject.get(key).toString())) {
                jsonObject.replace(key,jsonObject.get(key),"");
                payload.put(key, jsonObject.get(key));
            }
        }
        return payload;
    }
}
